/*=+--+=#=+--     Unmanned Aerial System Management Software      --+=#=+--+=#*\
|          Copyright (C) 2011 Regents of the University of Colorado.           |
|                             All Rights Reserved.                             |

     This program is free software: you can redistribute it and/or modify
     it under the terms of the GNU General Public License version 2 as
     published by the Free Software Foundation.

     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with this program.  If not, see <http://www.gnu.org/licenses/>.

            Jack Elston                       Cory Dixon                        
|           elstonj@colorado.edu              dixonc@colorado.edu              |
|                                                                              |
\*=+--+=#=+--                 --+=#=+--+=#=+--                    --+=#=+--+=#*/

/*!
  \file template.h
	\brief header file for template

                    \author Jack Elston
             $Date: 2006/10/25 13:34:45 $
*/

#ifndef __COOPTRACK_H
#define __COOPTRACK_H

#include <unistd.h>
#include <signal.h>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>

#include "String.h"
#include "command.h"
#include "queue.h"
#include "type_defs.h"
#include "threadedPipe.h"
#include "shmStruct.h"

#define ORBIT_RADIUS 200 
#define ORBIT_WAYPOINT 30 

#define TRACKING_ALT 1770
#define TRACKING_LOOP_PERIOD 10
#define CUSTODY_REQ_PERIOD 5

typedef enum { SEARCH=1, TRACK, REQ_CHILD, CHILD_COORDINATE, REM_TARGET, CHILD_UNCOORDINATE, REM_CHILD } FCMode;
typedef enum { CUSTODY_REQ=1, OTHER_CUSTODY, MY_CUSTODY, COORDINATE_REQ, COORDINATED, UNCOORDINATE_REQ, UNCOORDINATED, CUSTODY_DROP } ChildMode;
typedef enum { REQUESTING_TRACK=8, UNAVAILABLE, TRACKING } SAVMode;
typedef enum { STREAM_REQ=11, STREAM_REM } MNRMode;

class CoopTrack : public ThreadedPipe
{
private:
  ClientList tracked_vehicles;
  ClientList children;

  TimeStamp last_tracking_loop;
  TimeStamp last_custody_req;

  void findMAVs();
  void manageCustody();
  void trackTargets();
  void reqCustody(Client * a_client, bool toggle);
  void coordinateChildren(bool toggle);
  void childrenTrack(GPSLatLon_t orbit_point);
  void recruitSAVs();
  void transferTrack(Client * a_client);
  void finishTransfer();

  FCMode fc_mode;
  Client * pending_child;

public:
	CoopTrack(String name);
	~CoopTrack() {}
					 
	void update();
};

#endif
